package org.bigjava.novelplatform.controller;

import lombok.extern.slf4j.Slf4j;
import org.bigjava.novelplatform.common.utils.R;
import org.bigjava.novelplatform.service.OrderService;
import org.bigjava.novelplatform.vo.ChapterOrderVo;
import org.bigjava.novelplatform.vo.UserInfoVo;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author ykh
 * @create 2022-05-29 10:39
 */
@RestController
@RequestMapping("/api/order")
@Slf4j
public class OrderController {

    @Resource
    private OrderService orderService;

    /**
     * 购买章节
     * @param bookId
     * @param sort
     * @param userInfoVo
     * @return
     */
    @PostMapping("/purchaseSection/{bookId}/{sort}")
   public R purchaseSection(@PathVariable("bookId") Integer bookId,@PathVariable("sort") Integer sort,
                            UserInfoVo userInfoVo){
       orderService.purchaseSection(userInfoVo.getId(), bookId, sort);
       return R.ok();
   }


    /**
     * 打赏书籍
     * @param bookId
     * @param amountOfMoney
     * @param count
     * @param userInfoVo
     * @return
     */
    @PostMapping("/rewardBook/{bookId}/{amountOfMoney}/{count}")
    public R rewardBook(@PathVariable("bookId") Integer bookId,@PathVariable("amountOfMoney") BigDecimal amountOfMoney,
                        @PathVariable(value = "count",required = false) Integer count,
                             UserInfoVo userInfoVo){
        orderService.rewardBook(userInfoVo.getId(), bookId,amountOfMoney, count == null ? 1 : count);
        return R.ok();
    }

    /**
     * 获取章节内容，如果已订阅，直接购买
     * @param bookId
     * @param sort
     * @param userInfoVo
     * @return
     */
    @PostMapping("/selectChapterOrder/{bookId}/{sort}")
    public R selectChapterOrder(@PathVariable("bookId") Integer bookId,@PathVariable("sort") Integer sort,UserInfoVo userInfoVo){
        ChapterOrderVo chapterOrderVo = orderService.selectChapterOrder(userInfoVo.getId(), bookId, sort);
        return R.ok().setData(chapterOrderVo);
    }


}
